<template>
	<view class="warp">
		<rich-text :nodes="sysSetting.readDes" v-if="sysSetting.readDes"></rich-text>
		<view class="sign" @click="bookSign">签到</view>
		<wolf-shareing ref="shareRef"></wolf-shareing>
	</view>
</template>

<script>
	const db = uniCloud.database();
	import {
		mapState,
		mapActions
	} from 'pinia'
	import {
		useUserStore
	} from "@/pinia/user.js"
	import WolfShareing from "./components/shareing.vue"
	export default {
		name: "wolf-booking",
		components: {
			WolfShareing
		},
		data() {
			return {
				sign_date: 0,
				sign_times: 0,
				start_sign_date: 0 //开始打卡日期
			}
		},
		computed: {
			...mapState(useUserStore, ['userInfo', 'sysSetting']),
		},
		onLoad(op) {
			// console.log('op===>',op);
			if (op.sign_obj) {
				let obj = JSON.parse(op.sign_obj)
				this.sign_date = obj.sign_date || 0
				this.sign_times = obj.sign_times || 0
				this.start_sign_date = obj.start_sign_date || 0
				// console.log("obj===>",obj,this.sign_date,this.sign_times,this.start_sign_date);
			}
			this.loadMyTimes()
			
		},
		methods: {
			...mapActions(useUserStore, ['updateUserInfo']),
			async loadMyTimes() {
				if(!this.userInfo._id){
					return uni.navigateTo({
						url:'/uni_modules/uni-id-pages/pages/login/login-withoutpwd'
					})
				}
				uni.showLoading({
					mask:true,
					icon:'none'
				})
				let res = await db.collection('uni-id-users')
					.doc(this.userInfo._id)
					.field('sign_times,sign_date,start_sign_date,sign_score')
					.get()
				console.log("res=========>>>>",res.result.data);
				const {
					sign_times = 0, sign_date = 0, start_sign_date = 0 ,sign_score = 0
				} = res.result.data[0]
				this.sign_date = sign_date
				this.sign_times = sign_times
				this.start_sign_date = start_sign_date
				this.sign_score = sign_score
				console.log("===>", sign_times, sign_date, start_sign_date);
				uni.hideLoading()
			},
			async bookSign() {
				let now = new Date().getTime()
				if (this.sign_date && new Date(this.sign_date).setHours(0, 0, 0, 0) === new Date().setHours(0, 0, 0,
						0)) { //比较当天0点时间戳是否相等
					return this.share()
				}
				uni.showLoading({
					title: '提交中...'
				})
				let obj = {
					sign_times: this.sign_times + 1,
					sign_date: now
				}
				if (!this.start_sign_date) {
					obj.start_sign_date = now
				}
				await db.collection('uni-id-users').doc(this.userInfo._id).update(obj)

				//添加一条签到记录
				db.collection("wolf-read-sing-log").add({
					user_id: this.userInfo._id
				})
				//更新学习分与连续签到记录
				this.upSign(this.sign_date)
				
				this.sign_date = now
				this.sign_times = this.sign_times + 1
				uni.hideLoading()
				this.share()
				// console.log("bookSign", this.sign_times, this.sign_date);
			},
			//更新学习分， sign_score,sign_con
			upSign(sign_date) {
				// sign_date  上次打卡时间
				//判断今天的0点时间戳与签到的0点时间戳是是一天,说明是连续签到
				let obj = {}
				if (new Date().setHours(0, 0, 0, 0) - new Date(sign_date).setHours(0, 0, 0, 0) == 1 * 24 * 60 * 60 *
					1000) {
						obj.sign_con = this.userInfo.sign_con + 1
					if(this.userInfo.sign_con<=20){
						obj.sign_score = this.userInfo.sign_score + this.userInfo.sign_con * 5
					}else{
						obj.sign_score = this.userInfo.sign_score + 100
					}
					
				} else {
					obj.sign_con = 1
					obj.sign_score = this.userInfo.sign_score + 1
				}
				console.log("===>>",new Date().setHours(0, 0, 0, 0) - new Date(sign_date).setHours(0, 0, 0, 0) == 1 * 24 * 60 * 60 *
					1000);
				console.log('upSign===>>',new Date().setHours(0, 0, 0, 0),sign_date,new Date(sign_date).setHours(0, 0, 0, 0),obj);
				this.updateUserInfo(obj)
			},
			share() {
				uni.showModal({
					confirmText: "去分享",
					title: '分享',
					content: "今日已读",
					success: (res) => {
						if (res.confirm) {
							this.$refs.shareRef.share()
						}
					}
				})
			},
			navigator(){
				uni.navigateTo({
					url:"/pages/book/list"
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	view {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
	}

	.warp {
		padding: 20rpx;
	}

	.sign {
		margin: 20rpx auto;
		width: 200rpx;
		padding: 10rpx 20rpx;
		background-color: #962cf0;
		border-radius: 60rpx;
		text-align: center;
		color: #fff;
	}


	
</style>